package com.jun.ribbon.command;

import com.jun.ribbon.entity.User;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.web.client.RestTemplate;

/**
 * 描述:
 *
 * @outhor jun
 * @create 2018-04-23 14:34
 */
public class UserCommand extends HystrixCommand<User> {

    private RestTemplate restTemplate;
    private Long id;

    public UserCommand(Setter setter, RestTemplate restTemplate, Long id) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserGroup")));
        this.restTemplate = restTemplate;
        this.id = id;
    }

    @Override
    protected User run(){
        return restTemplate.getForObject("http://USER-SERVICE/users/{1}",User.class,id);
    }

    @Override
    protected User getFallback(){
        return new User();
    }

    @Override
    protected String getCacheKey() {
        return String.valueOf(id);
    }
}
